﻿/*--------------------------------------------------
 Creater：Junxuan.Su
 Time：2013-12-11 14:28:50
 Remark : Config
--------------------------------------------------*/

using System;
using System.Configuration;

namespace HXDNB.Infrastructure
{
    public class Config
    {
        /// <summary>
        /// 更新版本号
        /// </summary>
        private static int _version;
        public static int Version
        {
            get { return LastReleaseTime.Date == DateTime.Now.Date ? _version : 1; }
            set { _version = value; }
        }

        /// <summary>
        /// 最近一次更新时间
        /// </summary>
        public static DateTime LastReleaseTime { get; set; }

        /// <summary>
        /// Dtd文件路径
        /// </summary>
        public static string Dtd
        {
            get { return GetPath(GetConfig("Dtd", "/DTD/xhtml1-transitional.dtd")); }
        }

        /// <summary>
        /// 模版文件夹路径
        /// </summary>
        public static string Template
        {
            get { return GetPath(GetConfig("Template", "/Temp/")); }
        }

        /// <summary>
        /// 预览文件夹路径
        /// </summary>
        public static string Preview
        {
            get { return GetPath(GetConfig("Preview", "/Preview/")); }
        }

        /// <summary>
        /// 预览站点首页
        /// </summary>
        public static string HomePage
        {
            get { return GetConfig("HomePage", "Index.html"); }
        }

        /// <summary>
        /// 发布根目录
        /// </summary>
        public static string ReleaseRoot
        {
            get { return GetPath(GetConfig("Release", "/Release/")); }
        }

        /// <summary>
        /// 当前发布文件夹路径
        /// </summary>
        public static string Release
        {
            get
            {
                return GetPath(GetConfig("Release", "/Release/").TrimEnd('/') + string.Format("/{0}v{1}/", DateTime.Today.ToString("yyyyMMdd"), Version));
            }
        }

        /// <summary>
        /// Web站点根目录
        /// </summary>
        public static string WebRoot
        {
            get { return GetConfig("WebRoot", "/Web/"); }
        }

        /// <summary>
        /// 移动站点根目录
        /// </summary>
        public static string MobileRoot
        {
            get { return GetConfig("MobileRoot", "/Mobile/"); }
        }

        /// <summary>
        /// 日志文件夹路径
        /// </summary>
        public static string Log
        {
            get { return GetPath(GetConfig("Log", "/Log/")); }
        }

        /// <summary>
        /// 根据名称获取配置文件中的值
        /// </summary>
        /// <param name="key">名称</param>
        /// <returns>配置</returns>
        public static string GetConfig(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 根据名称获取配置文件中的值
        /// </summary>
        /// <param name="key">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置</returns>
        public static string GetConfig(string key, string defaultValue)
        {
            return ConfigurationManager.AppSettings[key] ?? defaultValue;
        }

        /// <summary>
        /// 获取路径
        /// </summary>
        /// <param name="path">初始路径</param>
        /// <returns>处理后路径</returns>
        private static string GetPath(string path)
        {
            if (path.IndexOf(@":/", StringComparison.Ordinal) == -1)
                return AppDomain.CurrentDomain.BaseDirectory + path.TrimStart('/');
            return path;
        }
    }
}
